package com.bugbuff.dubbo.server.api;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.bugbuff.dubbo.server.pojo.Logs;
import com.bugbuff.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.*;

@Service
public class LogsApiImpl implements LogsApi {

    //注入mongoTemplate
    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 查询日志，获取集合
     *
     * @return
     */
    @Override
    public List<Logs> queryLogs(Long uid, Integer page, Integer pagesize , String sortProp, String sortOrder) {
        List<Logs> logs = new ArrayList<>();
        switch (sortProp){
            //判断排序使用的字段
            case "logTime" : {
                //判断升降序方式，ascending 升序 descending 降序
                if (StrUtil.equals(sortOrder,"ascending")){
                    //分页并排序（升序）
                    PageRequest pageRequest = PageRequest.of(page - 1, pagesize,Sort.by(Sort.Order.asc("loginTime")));
                    //查询条件
                    Query query =Query.query(Criteria.where("uid").is(uid)).with(pageRequest);
                    //获取logs日志集合
                    logs = mongoTemplate.find(query, Logs.class);
                    //返回logs日志集合
                   break;

                }else {
                    //分页并排序（降序）
                    PageRequest pageRequest = PageRequest.of(page - 1, pagesize,Sort.by(Sort.Order.desc("loginTime")));
                    //查询条件
                    Query query =Query.query(Criteria.where("uid").is(uid)).with(pageRequest);
                    //获取logs日志集合
                    logs = mongoTemplate.find(query, Logs.class);
                    //返回logs日志集合
                    break;
                }
            }
            default:{
                //分页并排序（升序）
                PageRequest pageRequest = PageRequest.of(page - 1, pagesize,Sort.by(Sort.Order.asc("loginTime")));
                //查询条件
                Query query =Query.query(Criteria.where("uid").is(uid)).with(pageRequest);
                //获取logs日志集合
                logs = mongoTemplate.find(query, Logs.class);
                break;
            }
        }
        return logs;
    }

    /**
     * 获取最近登陆时间
     *
     * @param uid
     * @return
     */
    @Override
    public Logs queryLogs(Long uid) {
        //分页
        //查询条件
        //获取logs日志集合
        Query query = Query.query(Criteria.where("uid").is(uid)).with(Sort.by(Sort.Order.desc("loginTime")));
        List<Logs> logs = mongoTemplate.find(query, Logs.class);
        if (!ObjectUtil.isNotEmpty(logs))
            return null;
        return logs.get(0);
    }

    /**
     * 查询日志总数
     *
     * @param uid 用户id
     * @return
     */
    @Override
    public Long queryLogsCount(Long uid) {
        //根据用户uid查出所有的日志信息条数
        Query query = Query.query(Criteria.where("uid").is(uid));
        long count = mongoTemplate.count(query, Logs.class);
        //返回日志信息条数
        return count;
    }
}
